﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVCGenerator
{
    /// <summary>
    /// Base Model Class for Index Pages
    /// Includes Sort, and Paging Model fields.
    /// </summary>
    public class BasicIndexPage
    {
        private SpaceStackStringBuilder sb = new SpaceStackStringBuilder();

        public BasicIndexPage()
        {

            sb.AppendLine("using System;");
            sb.AppendLine();
            sb.AppendLine("public class BasicIndexPage");
            sb.AppendLine("{");
            sb.AddSpace();
            if (true)
            {
                sb.AppendLine();
                sb.AppendLine("public String SortColumn { get; set; }");
                sb.AppendLine("public String SortDirection { get; set; }");
                
                sb.AppendLine();
                sb.AppendLine("public int CurrentPage { get; set; }");
                sb.AppendLine("public int RecordCount { get; set; }");
                sb.AppendLine("public int RecordsPerPage { get; set; }");
                sb.AppendLine("public String PagingAction { get; set; }");
                sb.AppendLine();
                sb.AppendLine("public int TotalPages");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("get {");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("int CurrentTotalPages = 0;");
                        sb.AppendLine();
                        sb.AppendLine("// doing integer division ");
                        sb.AppendLine("int CurrentPages = RecordCount / RecordsPerPage;");
                        sb.AppendLine();
                        sb.AppendLine("int RemainingRecords = RecordCount % RecordsPerPage;");
                        sb.AppendLine();
                        sb.AppendLine("if (RemainingRecords == 0)");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("if (CurrentPages > 0)");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine("// perfectly divisible pages, Normally we would go back one, but the result of this function is one based.");
                                sb.AppendLine("CurrentTotalPages = CurrentPages;");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("else");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("// One based");
                            sb.AppendLine("CurrentTotalPages = CurrentPages + 1;");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("return CurrentTotalPages; ");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    sb.RemoveSpace();
                }
            }
            sb.AppendLine("}");
            sb.RemoveSpace();
            sb.AppendLine("}");
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
}
